'  ==========================================================================
'  |                                                                        |
'  | SQLitening Universal API                                               |
'  |                                                                        |
'  ==========================================================================
'   Contains routines, that can be called by almost any program, to
'   access a SQLite databases and flat files residing locally or
'   remotely (using SQLitening Server). This can support any
'   language that can call a standard Dll (including VB).  The
'   parameter passing is patterned after the Windows API.
'   SQLiteningU.Dll is a front-end to SQLitening.Dll. See
'   SQLitening.Txt or SQLitening.Chm for information on each
'   command.
'
'  ==========================================================================
'  PUBLIC DOMAIN SOFTWARE
'  The author or authors of this code dedicate any and all
'  copyright interest in this code to the public domain.
'  Anyone is free to copy, modify, publish, use, compile,
'  sell, or distribute the original code, either in source
'  code form or as a compiled binary, for any purpose,
'  commercial or non-commercial, and by any means.
'  Fred Meier - July 2010
'  ==========================================================================

'  2011-05-21 DremLIN.Ru - Start modifications
                                  
' For Build project as slsql.dll                                   
' %SLSQL = 1
' For Build Standart SQLiteningU.Dll
' %SLSQL = 0 or comment line 
%SLSQL = 1

' For Build ALL-in-1 library without use other dll
%SLSQL_ONLY = 1 

' For Build SQLiteningU.Dll - ALL-in-1 ANSI strings library
'%STDNAME_U = 1

#If Not %Def(%DebugEXE) 
   #Dim All
#EndIf

#If %Def(%SLSQL)
   #If Not %Def(%DebugEXE)
      #If %Def(%STDNAME_U) 
         #Compile Dll "..\Bin\SQLiteningU.dll"
      #Else
         #Compile Dll "..\Bin\libslsql.dll"
      #EndIf   
      #Include "SQLitening.bas"
   #Else
      #Include "..\Src\SQLitening.bas"
   #EndIf   
#Else 
   #If Not %Def(%DebugEXE) 
      #Compile Dll "..\Bin\SQLiteningU.Dll"  
   #EndIf   
   #Include "..\Inc\SQLitening.Inc"
#EndIf

'============================<[ Declares ]>============================
Declare Sub      CopyMem LIB "KERNEL32.DLL" Alias "RtlMoveMemory" (pDestination as Any, pSource as Any, byval rlLength as Long)

'=======================<[ Internal Ruts ]>========================
' ==========>>> Set Out Value Text
Function irSetOutValueText (rsInString as String, _
                            byval whOutValue as Dword, _
                            byref blSizeOfOutValue as Long, _
                            optional byval rlIgnoreErrorNumber as Long) as Long

   if (isfalse rlIgnoreErrorNumber and slGetErrorNumber) or blSizeOfOutValue <= len(rsInString) then
      blSizeOfOutValue = -1
   else
      blSizeOfOutValue = len(rsInString)
      CopyMem byval whOutValue, byval strptr(rsInString), blSizeOfOutValue + 1
      function = -1
   end if

End Function

' ==========>>> Set Out Value Binary
Function irSetOutValueBinary (rsInString as String, _
                              byval whOutValue as Dword, _
                              byref blSizeOfOutValue as Long) as Long

   if slGetErrorNumber or blSizeOfOutValue < len(rsInString) then
      blSizeOfOutValue = -1
   else
      blSizeOfOutValue = len(rsInString)
      CopyMem byval whOutValue, byval strptr(rsInString), blSizeOfOutValue
      function = -1
   end if

End Function

'=============================<[ Attach ]>=============================
Function sluAttach alias "sluAttach" (byref rsFileName as Asciz, _
                                      byref rsAsDatabaseName as Asciz, _
                                      byref rsModChars as Asciz)Export as Long

   function = slAttach(bycopy rsFileName, bycopy rsAsDatabaseName, rsModChars)

End Function

'==========================<[ BuildBindDat ]>==========================
Sub sluBuildBindDat alias "sluBuildBindDat" (byval rhData as Dword, _
                                             byval rlLenghtOfData as Long, _
                                             byval whBindDat as Dword, _
                                             byref blSizeOfBindDat as Long, _
                                             byref rsModChars as Asciz)Export

   Local lsData as String

   lsData = space$(rlLenghtOfData)
   CopyMem byval strptr(lsData), byval rhData, rlLenghtOfData
   irSetOutValueBinary slBuildBindDat(lsData, rsModChars), whBindDat, blSizeOfBindDat

End Sub

'======================<[ BuildInsertOrUpdate ]>=======================
Sub sluBuildInsertOrUpdate alias "sluBuildInsertOrUpdate" (byref rsTable as Asciz, _
                                                           byval rhValues as Dword, _
                                                           byval rlLenghtOfValues as Long, _
                                                           byref rsColumns as Asciz, _
                                                           byref rsWhere as Asciz, _
                                                           byval whSqlStatment as Dword, _
                                                           byref blSizeOfSqlStatement as Long)Export

   Local lsValues as String

   lsValues = space$(rlLenghtOfValues)
   CopyMem byval strptr(lsValues), byval rhValues, rlLenghtOfValues
   irSetOutValueText slBuildInsertOrUpdate(bycopy rsTable, lsValues, rsColumns, rsWhere), whSqlStatment, blSizeOfSqlStatement

End Sub

'============================<[ Connect ]>=============================
Function sluConnect alias "sluConnect" (byref rsServer as Asciz, _
                                        byval rlPort as Long, _
                                        byref rsModChars as Asciz, _
                                        byval whOutData as Dword, _
                                        byval blSizeOfOutData as Long)Export as Long

   Local llRC as Long
   Local lsOutData as String

   llRC = slConnect(rsServer, rlPort, rsModChars, lsOutData)
   if isfalse llRC and whOutData then irSetOutValueBinary lsOutData, whOutData, blSizeOfOutData
   function = llRC

End Function

'===========================<[ ConvertDat ]>===========================
Sub sluConvertDat alias "sluConvertDat" (byval rhDataIn as Dword, _
                                         byval rlLenghtOfDataIn as Long, _
                                         byval rhDataOut as Dword, _
                                         byref blSizeOfDataOut as Long, _
                                         byref rsModChars as Asciz)Export

   Local lsDataIn as String

   lsDataIn = space$(rlLenghtOfDataIn)
   CopyMem byval strptr(lsDataIn), byval rhDataIn, rlLenghtOfDataIn
   irSetOutValueBinary slConvertDat(lsDataIn, rsModChars), rhDataOut, blSizeOfDataOut

End Sub

'=========================<[ Copy Database ]>==========================
Function sluCopyDatabase alias "sluCopyDatabase" (byref rsDestinationFileName as Asciz, _
                                byref rsModChars as Asciz, _
                                byref rsDatabaseName as Asciz)Export as Long

   function = slCopyDatabase(bycopy rsDestinationFileName, rsModChars, rsDatabaseName)

End Function

'==============================<[ Exe ]>===============================
Function sluExe alias "sluExe" (byref rsStatement as Asciz, _
                                byref rsModChars as Asciz)Export as Long

   function = slExe(bycopy rsStatement, rsModChars)

End Function

'============================<[ Exe Bind ]>============================
Function sluExeBind alias "sluExeBind" (byref rsStatement as Asciz, _
                                        byval rhBindDats as Dword, _
                                        byval rlLengthOfBindDats as Long, _
                                        byref rsModChars as Asciz)Export as Long

   Local lsBindDats as String

   lsBindDats = space$(rlLengthOfBindDats)
   CopyMem byval strptr(lsBindDats), byval rhBindDats, rlLengthOfBindDats
   function = slExeBind(bycopy rsStatement, lsBindDats, rsModChars)

End Function

'===========================<[ Field Get ]>============================
Sub sluF alias "sluF" (byval rlColumnNumber as Long, _
                       byval whFieldValue as Dword, _
                       byref blSizeOfFieldValue as Long, _
                       byval rlSetNumber as Long)Export

   if isfalse irSetOutValueText(slF(rlColumnNumber, rlSetNumber), whFieldValue, blSizeOfFieldValue) then slF -1, rlSetNumber

End Sub

'=======================<[ Field Get Extended ]>=======================
Sub sluFX alias "sluFX" (byval rlColumnNumber as Long, _
                         byval whFieldValue as Dword, _
                         byref blSizeOfFieldValue as Long, _
                         byval rlSetNumber as Long, _
                         byref rsModChars as Asciz)Export

   if isfalse irSetOutValueText(slFX(rlColumnNumber, rsModChars, rlSetNumber), whFieldValue, blSizeOfFieldValue) then slFX -1, rsModChars, rlSetNumber

End Sub

'=========================<[ Field Name Get ]>=========================
Function sluFN alias "sluFN" (byref rsColumnName as Asciz, _
                              byval whFieldValue as Dword, _
                              byref blSizeOfFieldValue as Long, _
                              byval rlSetNumber as Long)Export as Long

   if isfalse irSetOutValueText(slFN(bycopy rsColumnName, rlSetNumber), whFieldValue, blSizeOfFieldValue) then slFN "", rlSetNumber

End Function

'====================<[ Field Name Get Extended ]>=====================
Function sluFNX alias "sluFNX" (byref rsColumnName as Asciz, _
                                byval whFieldValue as Dword, _
                                byref blSizeOfFieldValue as Long, _
                                byval rlSetNumber as Long, _
                                byref rsModChars as Asciz)Export as Long

   if isfalse irSetOutValueText(slFNX(bycopy rsColumnName, rsModChars, rlSetNumber), whFieldValue, blSizeOfFieldValue) then slFNX "", rsModChars, rlSetNumber

End Function

'========================<[ Get Change Count ]>========================
Function sluGetChangeCount Alias "sluGetChangeCount" (ByRef rsModChars As Asciz)Export As Long

   function = slGetChangeCount(bycopy rsModChars)

End Function

'========================<[ Get Column Name ]>=========================
Sub sluGetColumnName alias "sluGetColumnName" (byval rlColumnNumber as Long, _
                                               byval whColumnName as Dword, _
                                               byref blSizeOfColumnName as Long, _
                                               byval rlSetNumber as Long)Export

   irSetOutValueText slGetColumnName(rlColumnNumber, rlSetNumber), whColumnName, blSizeOfColumnName

End Sub

'=======================<[ Get Column Number ]>========================
Function sluGetColumnNumber alias "sluGetColumnNumber" (byref rsColumnName as Asciz, _
                                                        byval rlSetNumber as Long)Export as Long

   function = slGetColumnNumber(bycopy rsColumnName, rlSetNumber)

End Function

'===========================<[ Get Error ]>============================
Sub sluGetError alias "sluGetError" (byval whError as Dword, _
                                     byref blSizeOfError as Long)Export

   irSetOutValueText slGetError(), whError, blSizeOfError, -1

End Sub

'============================<[ Get File ]>============================
Function sluGetFile alias "sluGetFile" (byref rsFileName as Asciz, _
                                        byval whFileData as Dword, _
                                        byref blSizeOfFileData as Long, _
                                        byref rsModChars as Asciz)Export as Long

   Local llA as Long
   Local lsFileData as String

   llA = slGetFile(bycopy rsFileName, lsFileData, rsModChars)
   if llA then
      function = llA
   else
      if isfalse irSetOutValueBinary(lsFileData, whFileData, blSizeOfFileData) then  slGetFile "**FileDataTooSmall**", ""
   end if

End Function

'===========================<[ Get Handle ]>===========================
Function sluGetHandle alias "sluGetHandle" (byref rsModChars as Asciz, _
                                            byval rlSetNumber as Long)Export as Long

   function = slGetHandle(rsModChars, rlSetNumber)

End Function

'============================<[ Get Row ]>=============================
Function sulGetRow alias "sluGetRow" (byval rlSetNumber as Long, _
                                      byref rsModChars as Asciz)Export as Long

   function = slGetRow(rlSetNumber, rsModChars)

End Function

'===========================<[ Get Status ]>===========================
Sub sluGetStatus alias "sluGetStatus" (byval rlRequest as Long, _
                                       byval whStatus as Long, _
                                       byref blSizeOfStatus as Long) Export
   irSetOutValueText slGetStatus(rlRequest), whStatus, blSizeOfStatus

End Sub

'=====================<[ Get Table Column Names ]>=====================
Sub sluGetTableColumnNames alias "sluGetTableColumnNames" (byref rsTableName as Asciz, _
                                                           byval whColumnNames as Dword, _
                                                           byref blSizeOfColumnNames as Long) Export

   irSetOutValueBinary slGetTableColumnNames(bycopy rsTableName), whColumnNames, blSizeOfColumnNames

End Sub

'========================<[ Get Table Names ]>=========================
Sub sluGetTableNames alias "sluGetTableNames" (byref rsDataBase as Asciz, _
                                               byval whTableNames as Dword, _
                                               byref blSizeOfTableNames as Long) Export

   irSetOutValueBinary slGetTableNames(bycopy rsDataBase), whTableNames, blSizeOfTableNames

End Sub

'======================<[ Is Column Name Valid ]>======================
Function sluIsColumnNameValid alias "sluIsColumnNameValid" (byref rsColumnName as Asciz, _
                                                            byval rlSetNumber as Long)Export as Long

   function = slIsColumnNameValid(bycopy rsColumnName, rlSetNumber)

End Function

'==============================<[ Open ]>==============================
Function sluOpen alias "sluOpen" (byref rsFileName as Asciz, _
                                  byref rsModChars as Asciz) Export as Long

    function = slOpen(rsFileName, rsModChars)

End Function

'==========================<[ Pop Database ]>==========================
Sub sluPopDatabase alias "sluPopDatabase" (byval rhSave as Dword) Export

   Local lsSave as String

   if rhSave then
      lsSave = space$(21)
      CopyMem byval strptr(lsSave), byval rhSave, 21
      slPopDatabase lsSave
   else
      slPopDatabase
   end if

End Sub

'============================<[ Pop Set ]>=============================
Sub sluPopSet alias "sluPopSet" (byval rlSetNumber as Long, _
                                 byval rhSave as Dword, _
                                 byval rlLenghtOfSave as Long) Export
   Local lsSave as String

   if rhSave then
      lsSave = space$(rlLenghtOfSave)
      CopyMem byval strptr(lsSave), byval rhSave, 21
      slPopSet rlSetNumber, lsSave
   else
      slPopSet rlSetNumber
   end if

End Sub

'=========================<[ Push Database ]>==========================
Sub sluPushDatabase alias "sluPushDatabase" (byval whSave as Dword) Export

   Local lsSave as String

   if whSave then
      slPushDatabase lsSave
      irSetOutValueBinary lsSave, whSave, 21
   else
      slPushDatabase
   end if

End Sub

'============================<[ Push Set ]>============================
Sub sluPushSet alias "sluPushSet" (byval rlSetNumber as Long, _
                                   byval whSave as Dword, _
                                   byref blSizeOfSave as Long) Export
   Local lsSave as String

   if whSave then
      slPushSet rlSetNumber, lsSave
      irSetOutValueBinary lsSave, whSave, blSizeOfSave
   else
      slPushSet rlSetNumber
   end if

End Sub

'============================<[ Put File ]>============================
Function sluPutFile alias "sluPutFile" (byref rsFileName as Asciz, _
                                        byval rhFileData as Dword, _
                                        byval rlLenghtOfFileData as Long, _
                                        byref rsModChars as Asciz)Export as Long

   Local lsFileData as String

   lsFileData = space$(rlLenghtOfFileData)
   CopyMem byval strptr(lsFileData), byval rhFileData, rlLenghtOfFileData
   function = slPutFile(bycopy rsFileName, lsFileData, rsModChars)

End Function

'============================<[ Run Proc ]>============================
Function sluRunProc alias "sluRunProc" (byref rsProcName as Asciz, _
                                        byref blParm1 as Long, _
                                        byref blParm2 as Long, _
                                        byval bhParm3 as Dword, _
                                        byval rlLengthOfParm3 as Long, _
                                        byref blSizeOfParm3 as Long, _
                                        byval bhParm4 as Dword, _
                                        byval rlLengthOfParm4 as Long, _
                                        byref blSizeOfParm4 as Long, _
                                        byref rsModChars as Asciz)Export as Long

   Local lsParm3, lsParm4 as String

   lsParm3 = space$(rlLengthOfParm3)
   CopyMem byval strptr(lsParm3), byval bhParm3, rlLengthOfParm3
   lsParm4 = space$(rlLengthOfParm4)
   CopyMem byval strptr(lsParm4), byval bhParm4, rlLengthOfParm4
   function= slRunProc(bycopy rsProcName, blParm1, blParm2, lsParm3, lsParm4)
   irSetOutValueBinary lsParm3, bhParm3, blSizeOfParm3
   irSetOutValueBinary lsParm4, bhParm4, blSizeOfParm4

End Function

'==============================<[ Sel ]>===============================
Function sluSel alias "sluSel" (byref rsStatement as Asciz, _
                                byval rlSetNumber as Long, _
                                byref rsModChars as Asciz)Export as Long

   function = slSel(bycopy rsStatement, rlSetNumber, rsModChars)

End Function

'============================<[ Sel Str ]>=============================
Sub sluSelStr alias "sluSelStr" (byref rsStatement as Asciz, _
                                 byref rsModChars as Asciz, _
                                 byval whFieldsAndRows as Dword, _
                                 byref blSizeOfFieldsAndRows as Long) Export

   irSetOutValueBinary slSelStr(bycopy rsStatement, rsModChars), whFieldsAndRows, blSizeOfFieldsAndRows

End Sub

'========================<[ Set Process Mods ]>========================
Sub sluSetProcessMods alias "sluSetProcessMods" (byref rsModChars as Asciz)Export

   slSetProcessMods bycopy rsModChars

End Sub
'======================<[ Sel Rel Named Locks ]>=======================
Function sluSetRelNamedLocks Alias "sluSetRelNamedLocks" (ByRef rsLockNames As Asciz, _
                                                           ByRef rsModChars As Asciz, _
                                                           ByRef rsSelStatement As Asciz, _
                                                           ByVal rlSelSetNumber As Long, _
                                                           ByRef rsSelModChars As Asciz)Export As Long

   function = slSetRelNamedLocks(bycopy rsLockNames, rsModChars, rsSelStatement, rlSelSetNumber, rsSelModChars)

End Function


